import { queryCurrent, query as queryUsers } from '@/services/user';
const CategoryModel = {
  namespace: 'category',
  state: {
    cateData: [
      {
        value: '1',
        label: '食品',
        children: [
          {
            value: '1-1',
            label: '饮料',
            children: [
              {
                value: '1-2-1',
                label: '酸奶',
              },
              {
                value: '1-2-2',
                label: '鲜牛奶',
              },
              {
                value: '1-2-3',
                label: '纯牛奶',
              },
            ],
          },
        ],
      },
      {
        value: '2',
        label: '服装',
        children: [
          {
            value: '2-1',
            label: '男装',
            children: [
              {
                value: '2-2-1',
                label: '上衣',
              },
              {
                value: '2-2-2',
                label: '下装',
              },
            ],
          },
          {
            value: '2-2',
            label: '女装',
          },
        ],
      },
    ],
  },
  effects: {
    *fetch(_, { call, put }) {
      const response = yield call(queryUsers);
      yield put({
        type: 'save',
        payload: response,
      });
    },

    *fetchCurrent(_, { call, put }) {
      const response = yield call(queryCurrent);
      yield put({
        type: 'saveCurrentUser',
        payload: response,
      });
    },
  },
  reducers: {
    saveCurrentUser(state, action) {
      return { ...state, currentUser: action.payload || {} };
    },

    changeNotifyCount(
      state = {
        currentUser: {},
      },
      action,
    ) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          notifyCount: action.payload.totalCount,
          unreadCount: action.payload.unreadCount,
        },
      };
    },
  },
};
export default CategoryModel;
